from django.db import models
from django.core.validators import RegexValidator


# Create your models here.


class Active(models.Model):  # 作为基类不生成字段
    """ 状态表 """
    active = models.SmallIntegerField(verbose_name='激活状态', default=1, choices=((1, '激活'), (0, '删除')))

    class Meta:  # abstract = True 不在生成表仅生成字段
        abstract = True


class Administrator(Active):
    """ 管理员表 """
    username = models.CharField(verbose_name='用户名', max_length=10, db_index=True)
    password = models.CharField(verbose_name='密码', max_length=100, )
    mobile = models.CharField(verbose_name='手机号', max_length=11, db_index=True)
    create_date = models.DateTimeField(verbose_name='创建日期', auto_now_add=True)

    # 对象的内置方法，默认返回对象的字符串表示
    def __str__(self):
        return self.username


class Level(Active):
    """ 等级表 """
    title = models.CharField(verbose_name='标题', max_length=10, db_index=True)
    discount = models.IntegerField(verbose_name='折扣')

    def __str__(self):
        return self.title


class Customer(Active):
    """ 客户表 """
    username = models.CharField(verbose_name='用户名', max_length=10, db_index=True)
    password = models.CharField(verbose_name='密码', max_length=100, )
    mobile = models.CharField(verbose_name='手机号', max_length=11, db_index=True,
                              validators=[RegexValidator(r'^\d{11}$', '请输入正确的手机号')])
    balance = models.DecimalField(verbose_name='余额', max_digits=10, decimal_places=2, default=0)
    # limit_choices_to={'active': 1} 表示在关联字段的选择器中，筛选 active 字段值为1的记录。
    # level = models.ForeignKey(verbose_name='折扣', to='Level', on_delete=models.CASCADE, null=True, blank=True,
    #                           limit_choices_to={'active': 1})
    level = models.ForeignKey(verbose_name='折扣', to='Level', on_delete=models.CASCADE, null=True, blank=True)
    createdBy = models.ForeignKey(verbose_name='邀请人', to=Administrator, on_delete=models.CASCADE)
    create_date = models.DateTimeField(verbose_name='创建日期', auto_now_add=True)


class PriceStrategy(models.Model):
    """ 价格策略 （原价后续根据不同的等级做折扣） """
    count = models.IntegerField(verbose_name='数量', default=0)
    price = models.DecimalField(verbose_name='价格', max_digits=10, decimal_places=2, default=0)


class Order(Active ):
    """ 订单表 """
    status_choices = (
        (1, "待执行"),
        (2, "正在执行"),
        (3, "已完成"),
        (4, "失败"),
        (5, "已撤单"),
    )
    status = models.SmallIntegerField(verbose_name="状态", choices=status_choices, default=1)

    # 202211022123123123
    oid = models.CharField(verbose_name="订单号", max_length=64, unique=True)
    url = models.URLField(verbose_name="视频地址", db_index=True)
    count = models.IntegerField(verbose_name="数量")

    price = models.DecimalField(verbose_name="价格", default=0, max_digits=10, decimal_places=2)
    real_price = models.DecimalField(verbose_name="实际价格", default=0, max_digits=10, decimal_places=2)

    old_view_count = models.CharField(verbose_name="原播放量", max_length=32, default="0")

    create_datetime = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    customer = models.ForeignKey(verbose_name="客户", to="Customer", on_delete=models.CASCADE)
    memo = models.TextField(verbose_name="备注", null=True, blank=True)


class TransactionHistory(Active):
    """ 交易记录 """
    # 调用显示时用get_字段名 _display
    charge_type_class_mapping = {
        1: "success",
        2: "danger",
        3: "default",
        4: "info",
        5: "primary",
    }
    charge_type_choices = ((1, "充值"), (2, "扣款"), (3, "创建订单"), (4, "删除订单"), (5, "撤单"),)
    charge_type = models.SmallIntegerField(verbose_name="类型", choices=charge_type_choices)
    customer = models.ForeignKey(verbose_name='订单客户', to='Customer', on_delete=models.CASCADE)
    price = models.DecimalField(verbose_name='交易金额', max_digits=10, decimal_places=2)
    admin = models.ForeignKey(verbose_name='管理员', to='Administrator', on_delete=models.CASCADE, null=True,
                              blank=True)
    oid = models.CharField(verbose_name='订单号', max_length=64, null=True, blank=True, db_index=True)
    remarks = models.CharField(verbose_name='备注', max_length=500, null=True, blank=True)
    create_date = models.DateTimeField(verbose_name='创建日期', auto_now_add=True)
